// $codepro.audit.disable lossOfPrecisionInCast, numericLiterals
/**
 * Class: LOOKUP PANEL
 * Description: 
 * 
 */
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;



import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;


/**
 * This is the panel used for searching for a patient by name.
 * @author Christian
 * @version $ Revision 1.0 $
 *
 */
public class LookupPanel extends JPanel{
	/**
	 * Field txtSearchString.
	 */
	private final JTextField txtSearchString;

	/**
	 * Field btnSearch.
	 */
	private final JButton btnSearch;

	/**
	 * Field lblInstructions.
	 */
	private final JLabel lblInstructions;

	/**
	 * Field myLayout.
	 */
	private FlowLayout MyLayout; // $codepro.audit.disable variableShouldBeFinal

	/**
	 * Field myDataManager.
	 */
	private DataManager MyDataManager;   // $codepro.audit.disable variableShouldBeFinal -->

	/**
	 * Field A_ASCII_VALUE
	 */
	private static final int A_ASCII_VALUE = 65;

	/**
	 * Constructor for LookupPanel.
	 * @param myDataManager DataManager
	 */
	public LookupPanel(DataManager myDataManager){
		setBackground(GlobalVars.BACKGROUND);
		lblInstructions = new JLabel("Enter in the patients first name: ");
		this.MyDataManager = myDataManager;
		txtSearchString = new JTextField("", 20);
		txtSearchString.addKeyListener(new KeyAdapter() {
	
			public void keyPressed(KeyEvent arg0) { // Add's support for enter key
				if (arg0.getKeyCode() == KeyEvent.VK_ENTER) {
					searchButtonPressed();
				}
			}
		});
		
		btnSearch = new JButton("Search");
		btnSearch.addActionListener(new ActionListener() {
			
			public void actionPerformed(ActionEvent arg0) { 
				searchButtonPressed();
			}
		});
				
		MyLayout = new FlowLayout();
		MyLayout.addLayoutComponent("Instructions", lblInstructions);
		MyLayout.addLayoutComponent("Search Text Box", txtSearchString);
		MyLayout.addLayoutComponent("Search Button", btnSearch);
		add(lblInstructions);
		add(txtSearchString);
		add(btnSearch);
		
		setLayout(MyLayout);
	}

	/**
	 * Handles when the search button is pressed
	 */
	private void searchButtonPressed(){
		final String rawText = txtSearchString.getText();
		final String noJunkText = filterJunk(rawText); //Filter out commas
		final String[] wordsToSearch = getNames(noJunkText);		//Split words into an array
		final int[] rowNumbers = getRowNumbers(wordsToSearch);	//Return an array of row numbers to lookup.
		final Patient[] patientsArray = searchPatients(rowNumbers); //Lookup and return patients using the words
		
		for (int i = 0; i < patientsArray.length; i++) {
			PatientInformationPanel.createWindow(MyDataManager, patientsArray[i]); // Open up a Patient Information Panel for that patient // $codepro.audit.disable com.instantiations.assist.eclipse.analysis.unusedReturnValue
		}

		//Search first and last name columns for words found and return usernames
		//Return List of patients that have the username listed above
	}
	
	/**
	 * Method searchPatients.
	 * @param rowNumbers int[]
	 * @return Patient[]
	 */
	private Patient[] searchPatients(int[] rowNumbers){
		final Patient[] patientsArray = new Patient[rowNumbers.length];
		final String[][] rawData = new String[rowNumbers.length][];
		/*final List patientsList = new ArrayList();*/   // not used in any other part of code
		for (int i = 0; i < rowNumbers.length; i++){
			rawData[i] = MyDataManager.getDataAtRow(
						GlobalVars.PATIENT_SHEET,
						1,
						12,
						rowNumbers[i]
						 );
			patientsArray[i] = new Patient(rawData[i]); 
		}
		return patientsArray;
	}

	/**
	 * Method getRowNumbers.
	 * @param wordsToSearch String[]
	 * @return int[]
	 */
	private int[] getRowNumbers(String[] wordsToSearch){
		final int[] rowNumbers;
		final String firstName = wordsToSearch[0]; //Currently we're only using the first name entered in the search bar.
		rowNumbers = MyDataManager.getRowNumbers(GlobalVars.PATIENT_SHEET, 2, firstName); //Column Number 2 is First name
		return rowNumbers;
	}

	/**
	 * Method getNames.
	 * @param noJunkText String
	 * @return String[]
	 */
	private String[] getNames(String noJunkText){
		final String[] arrStrings = new String[5];
		final char[] arrChars = noJunkText.toCharArray();
		int wordCounter = 0;
		int asciiValue = A_ASCII_VALUE;	//Initialize Ascii value to A
		int prevValue;
		String myString = "";
		//StringBuffer myString = null;
		for (int i = 0; i < noJunkText.length(); i++){
			prevValue = asciiValue;
			asciiValue = (int) arrChars[i];
			if ((asciiValue > 64 && asciiValue < 91)  || (asciiValue > 96 && asciiValue < 123)){	//If it's a valid letter do this.
				myString += (char) asciiValue;
				//myString.append((char) asciiValue);
				}
			else if ((prevValue > 64 && prevValue < 91)  || (prevValue > 96 && prevValue < 123)){	//If the previous value is a valid letter do this
				//If the last value was a letter and this isn't, then this is the first space after a word
				if (wordCounter > 4){
					return arrStrings;
				}
				arrStrings[wordCounter] = myString;	//Add the current full word to the array
				//arrStrings[wordCounter] = myString.substring(0,myString.length()-1);
				wordCounter++;
				myString = "";				//Reset the value of myString
				//myString = null;
			}
			
		}
				
		return arrStrings;
	}

	/**
	 * Method filterJunk.
	 * @param rawText String
	 * @return String
	 */
	private String filterJunk(String rawText){
		String myString = "";
		final char[] myChars = rawText.toCharArray();
		//char[] finalChars = new char[rawText.length()];
		int asciiValue = 0;
		for (int i=0; i < myChars.length; i++){
			asciiValue = (int) myChars[i];	//The ascii value of this character 
			if ((asciiValue > 64 && asciiValue < 91)  || (asciiValue > 96 && asciiValue < 123)){	//If it's a valid letter do this.
				myString += (char) asciiValue;
				}
			else {
				myString += (char) 32;	//If it's not a letter, make it a space
			}
		}
		myString += (char) 32; //Tack on a space at the end to show the end of the word
		return myString;
	}

	/**
	 * Method createWindow.
	 * @param name String
	 * @param datamanager DataManager
	 * @return JFrame
	 */
	public static JFrame createWindow(String name, DataManager datamanager){
		final JFrame lookupFrame = new JFrame();
		/*final JPanel lookupPanel = new JPanel();*/  // not used in any other part of code
		lookupFrame.setSize(340, 160);
		lookupFrame.setTitle("Lookup Patient");
		lookupFrame.getContentPane().add(new LookupPanel(datamanager));
		lookupFrame.setVisible(true);
		return lookupFrame;
	}
}
